<?php $this->_extends('_layouts/admin/main_layout'); ?>

<?php $this->_block('resource'); ?>

<?php $this->_endblock(); ?>

<?php $this->_block('title'); ?>
	Html Templates
<?php $this->_endblock(); ?>

<?php $this->_block('contents'); ?>

<div id="content" class="box">
	<h1>搜索结果 - Html模板</h1>
	
	<div id="menu" class="box">
		<ul class="box">
			<li><a href="<?php echo url('adminhtmltemplates/index');?>"><span>已添加的Html模板</span></a></li>
		</ul>
	</div>
	
	<fieldset>
		<legend>搜索结果 - Html模板</legend>
		<?php if(!empty($htmlTemplatesUnAdded)): foreach($htmlTemplatesUnAdded as $root => $detail): ?>
			<fieldset>
				<legend class="root_folder"><?php echo $root; ?></legend>
				<a class="add_btn">添加此模板</a>
				<a class="hideshowdetail">收起</a>
				<div class="detail">
					<?php if(!empty($detail['folder'])): ?>
						<dl>
							<dt class="f-left">文件夹:</dt>
							<dd>
								<ul class="nostyle">
									<?php foreach($detail['folder'] as $folder): ?>
										<li class="f-left folders"><?php echo $folder; ?></li>
									<?php endforeach; ?>
								</ul>
							</dd>
						</dl>
					<?php endif; ?>
					
					<?php 
						if(!empty($detail['file'])):
						$fileClassify = $htmlTemplateModel->fileClassify($detail['file']);
						foreach($fileClassify as $class => $files):
					?>
						<dl>
							<dt class="f-left"><?php echo $class ?>:</dt>
							<dd>
								<ul class="nostyle">
									<?php foreach($files as $file): ?>
										<li class="f-left files">
											<?php 
												if($class == '图像'){?>
													<div style="width: 100px;">
														<img src='<?php echo $htmltemp_url . $root . '/' . $file; ?>' alt="<?php echo $file; ?>" width="100" height="100" />
														<span><?php echo $file; ?></span>
													</div>
											<?php }else{
													echo $file; 
												}
											?>
										</li>
									<?php endforeach; ?>
								</ul>
							</dd>
						</dl>
					<?php endforeach; endif; ?>
				</div>
			</fieldset>
		<?php endforeach; else: ?>
			没有找到未添加的模板
		<?php endif; ?>
	</fieldset>
</div>

<form id="add_block">
	<ul class="formErrors" id="addErrors">
	
	</ul>

	<table>
		<tr>
			<th id="add_foldername">asdasd</th>
		</tr>
		<tr>
			<td>模板名称:</td>
			<td>
				<input id="add_name" name="form[name]" />
			</td>
		</tr>
		<tr>
			<td>描述:</td>
			<td>
				<textarea id="add_desc" name="form[description]"></textarea>
			</td>
		</tr>
		<tr>
			<td>根目录文件夹:</td>
			<td>
				<input id="add_rootfolder" name="form[root_foldername]" />
			</td>
		</tr>
		<tr>
			<td>预览图（小）:</td>
			<td>
				<select id="add_thum_s" name="form[thumb_small]"></select>
			</td>
		</tr>
		<tr>
			<td>预览图（大）:</td>
			<td>
				<select id="add_thum_b" name="form[thumb_big]"></select>
			</td>
		</tr>
		<tr>
			<td>预览主页:</td>
			<td>
				<select id="add_previewindex" name="form[preview_indexpage]"></select>
			</td>
		</tr>
		<tr>
			<td>下载包:</td>
			<td>
				<select id="add_downloadpack" name="form[download_pack]"></select>
			</td>
		</tr>
		<tr>
			<td>区域分类:</td>
			<td>
				<select id="add_type_area" name="form[type_area_id]">
					<?php if(!empty($areaTypes)): foreach($areaTypes as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>颜色分类:</td>
			<td>
				<select id="add_type_color" name="form[type_color_id]">
					<?php if(!empty($colorTypes)): foreach($colorTypes as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>行列分类:</td>
			<td>
				<select id="add_type_columnwidth" name="form[type_columnwidth_id]">
					<?php if(!empty($columnwidthTypes)): foreach($columnwidthTypes as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>侧边栏分类:</td>
			<td>
				<select id="add_type_sidebarcorner" name="form[type_sidebarcorner_id]">
					<?php if(!empty($sidebarcornerTypes)): foreach($sidebarcornerTypes as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>用途分类:</td>
			<td>
				<select id="add_type_using" name="form[type_using_id]">
					<?php if(!empty($usingTypes)): foreach($usingTypes as $type): ?>
						<option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?>(<?php echo $type['name_en']; ?>)</option>
					<?php endforeach; endif; ?>
				</select>
			</td>
		</tr>
	</table>
</form>





<script type="text/javascript">
	$('#add_block').dialog({
		autoOpen: false,
		height: 'auto',
		width: 'auto',
		modal: true,
		buttons: {
			添加: function(){
				$.ajax({
					url: '<?php echo url('adminhtmltemplates/searchnew'); ?>',
					type: 'post',
					data: $('#add_block').serialize(),
					success: function(ret){
						if(ret.result){
							$('.add_btn.currAdd').unbind('click').text('已添加');
							$('.add_btn.currAdd').next('.hideshowdetail').click();
							$('#add_block').dialog( "close" );
						}else{
							$('#addErrors').html('');
							$.each(ret.errorinfo, function(item, errors){
								var erritems = $('<li />');
								erritems.append(item);
								var ul = $('<ul />');
								$.each(errors, function(k, error){
									ul.append('<li class="error">'+error+'</li>');
								});
								erritems.append(ul);
								$('#addErrors').append(erritems);
							});
						}
					},
					dataType: 'json'
				});
			},
			
			取消: function(){
				$('.currAdd').removeClass('currAdd');
				$(this).dialog( "close" );
			}
		},
		close: function(){
			$('.currAdd').removeClass('currAdd');
		}
	});

	$('.add_btn').click(function(){
		$(this).addClass('currAdd');
		
		var folders = $(this).parent().find('li.folders');
		var files = $(this).parent().find('li.files');
		var folder_root = $(this).prev('.root_folder').text();

		$('#add_block #add_foldername').text(folder_root);
		$('#add_block #add_name').val(folder_root);
		$('#add_block #add_desc').val('');
		$('#add_block #add_rootfolder').val(folder_root);
		$('#add_block #add_thum_s').html('');
		$('#add_block #add_thum_b').html('');
		$('#add_block #add_previewindex').html('');
		$('#add_block #add_downloadpack').html('');

		var imgExts = new Array('png', 'jpg', 'jpeg', 'bmp', 'gif');
		var indexPages = new Array('index', 'main');
		var pageExts = new Array('php', 'htm', 'html');
		var zipExts = new Array('rar', 'zip');
		$.each(files, function(k, v){
			var value = $(v).text();
			value = value.trim();   
			
			var ext = getFileExt(value);
			if(imgExts.hasVal(ext)){
				var imgObj = new Image();
				imgObj.src = $(v).find('img')[0].src
				
				$('#add_block #add_thum_s').append('<option value="'+value+'">('+imgObj.width+'*'+imgObj.height+')'+value+'</option>');
				$('#add_block #add_thum_b').append('<option value="'+value+'">('+imgObj.width+'*'+imgObj.height+')'+value+'</option>');
			}

			if(pageExts.hasVal(ext)){
				$('#add_block #add_previewindex').append('<option value="'+value+'">'+value+'</option>');
				if(indexPages.hasVal(getFileNameNoExt(value))){
					$('#add_block #add_previewindex').val(value);
				}
			}

			if(zipExts.hasVal(ext)){
				$('#add_block #add_downloadpack').append('<option value="'+value+'">'+value+'</option>');
			}
		});
		$('#addErrors').html('');
		
		$('#add_block').dialog('open');
	});

	$('.hideshowdetail').click(function(){
		var thisObj = $(this);
		var detail = $(this).next('.detail');
		detail.slideToggle('fast', function(){
			if(detail.css('display') == 'none'){
				thisObj.text('展开');
			}else{
				thisObj.text('收起');
			}
		});
	});
</script>
<?php $this->_endblock(); ?>